/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.service.singleton {
	import com.googlecode.milib.MILibMain;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.tools.Averager;
	import com.googlecode.milib.util.MilibUtil;

	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.utils.getTimer;

	/**
	 * @author Marek Brun
	 */
	public class EnterFrameService extends MLBroadcastClass {
		
		public const event_EnterFrame : String = 'EnterFrame';
		
		private static var instance : EnterFrameService;
		private var recivers : Array; //of MIObjSoul
		private var lastFrameTime : Number;
		public var enterframe_IDBlankObject : Object={name:'IDBlankObject'};
		private var betweenFramesTimeAverager : Averager;
		private var currentTime : Number;
		private var frameNumber :Number;
		private var root:DisplayObjectContainer;
		private var a_CallMethodAfterFrames:Array=[];
		
		public function EnterFrameService() {
			frameNumber=0;
			root=MILibMain.getInstance().getRoot();
			
			lastFrameTime=getTimer();
			recivers=[];
			betweenFramesTimeAverager=new Averager(root.stage.frameRate);
			betweenFramesTimeAverager.push(200);
			betweenFramesTimeAverager.push(200);
			currentTime=getTimer();
			root.addEventListener(Event.ENTER_FRAME, onRootEnterFrame, false, 0, true);
			
		}
		
		public function getLastFrameTime():Number {
			return betweenFramesTimeAverager.getAddedRecently();
		}
		
		public function getLastFrametimeInS01():Number {
			return betweenFramesTimeAverager.getAddedRecently()/1000;
		}
		
		public function getAverageFrameTime():Number {
			return betweenFramesTimeAverager.getAverage();
		}
		
		public function getAverageFrameTimeInS01():Number {
			return betweenFramesTimeAverager.getAverage()/1000;
		}
		
		public function getFPS():Number {
			return 1000/betweenFramesTimeAverager.getAverage();
		}
		
		public function getTime():Number {
			return currentTime;
		}
		
		public function pause():void {
			root.removeEventListener(Event.ENTER_FRAME, onRootEnterFrame);
		}
		
		public function unpause():void {
			lastFrameTime=currentTime=getTimer();
			root.addEventListener(Event.ENTER_FRAME, onRootEnterFrame, false, 0, true);
		}
		
		/** @return singleton instance of EnterFrameBroadcaster */
		public static function getInstance() : EnterFrameService {
			if (instance == null)
				instance = new EnterFrameService();
			return instance;
		}
		
		public function getFrameNumber():Number {
			return frameNumber;
		}
		
		public function callMethodAfterFrames(framesCount:Number, metod:Function, args:Array=null):void {
			if(args==null){ args=[]; }
			a_CallMethodAfterFrames.push([framesCount, metod, args]);
		}
		
//****************************************************************************
// EVENTS for EnterFrameBroadcaster
//****************************************************************************
		private function onRootEnterFrame(event:Event):void {
			frameNumber++;
			currentTime=getTimer();
			betweenFramesTimeAverager.push(currentTime-lastFrameTime);
			lastFrameTime=currentTime;
			broadcastEvent(event_EnterFrame);
			var i:uint;
			var loop:Array;
			for(i=0;i<a_CallMethodAfterFrames.length;i++){
				loop=a_CallMethodAfterFrames[i];
				loop[0]--;
				if(loop[0]<1){
					a_CallMethodAfterFrames.splice(i, 1);
					i--;
					MilibUtil.callMethod(loop[1], loop[2]);
				}
			}
		}
	}
}
